package com.desire.test;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.GeneralPath;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;

/**
 * @author desire
 */
public class _25_imageio图像操作 {

    public static void main1(String[] args) throws IOException {

        // 灰度图片
        String f = "C:\\Users\\desire\\Desktop\\javaProject\\01-javaBase\\src\\main\\resources\\刻晴.png";//xxx_gray.jpg  abc.png  abc_gray.png
        // 读取原来的图像
        BufferedImage i = ImageIO.read(new File(f));
        int w = i.getWidth();
        int h = i.getHeight();

        //建立新图像，使用黑白效果
        // 1 真彩 2 透明度 10 灰度
        BufferedImage ii = new BufferedImage(w, h, 10);
        Graphics2D g = ii.createGraphics();

        g.drawImage(i, 0, 0, w, h, null);

        g.setColor(new Color(0, 255, 0, 55));
        g.fillRect(0, 0, w, h);


        g.dispose();

        String ext = f.substring(f.lastIndexOf("."));
        String name = f.substring(0, f.lastIndexOf("."));
        String newname = name + "_gray" + ext;

        ImageIO.write(ii, "png", new File(newname));
    }

    public static void main(String[] args) {
        int h = 800;
        int w = 800;
        // 建立缓冲图像
        BufferedImage i = new BufferedImage(800, 800, 2);
        // 根据缓冲图像 建立画笔
        Graphics2D g = i.createGraphics();
        //设置线条和文件的，抗锯齿（平滑）
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_DEFAULT);

        //设置画笔颜色
        g.setColor(Color.gray);
        //画一个矩形
        g.fillRect(0, 0, 400, 400);

        // 画线  BasicStroke控制线条粗细
        g.setStroke(new BasicStroke(2));
        g.setColor(Color.YELLOW);
        g.drawLine(200, 0, 200, 400);
        g.drawLine(0, 200, 400, 200);

        // 画不填充的矩形
        g.setStroke(new BasicStroke(1));
        g.setColor(Color.green);
        g.drawRect(75, 75, 250, 250);

        //画填充颜色的矩形
        g.setColor(Color.MAGENTA);
        g.fillRect(150, 150, 100, 100);

        // 画空心圆
        g.setColor(Color.PINK);
        g.setStroke(new BasicStroke(3));
        g.drawOval(100, 100, 50, 50);
        // 画实心圆
        g.setColor(Color.PINK);
        g.setStroke(new BasicStroke(3));
        g.fillOval(150, 150, 50, 50);

        Random rand = new Random();
        for (int i1 = 0; i1 <= 200; i1++) {

            g.setColor(new Color(255, 0, 0, 50));
            int x = rand.nextInt(600);
            int y = rand.nextInt(400);
            int sw = rand.nextInt(100);
            int sh = rand.nextInt(100);
            g.fillOval(x, y, sh, sh);//实心圆
            g.setColor(new Color(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255), rand.nextInt(50) + 50));
            g.fillOval(x, y, sw, sh);//  椭圆
        }

        // 画虚线
        g.setStroke(new BasicStroke(3));
        g.setColor(Color.WHITE);
        for (int n = 0; n < 800; n++) {
            int x = n;
            n += 18;
            g.drawLine(x, 400, x + 5, 400);
            g.drawLine(400, x, 400, x + 5);
        }


        // 画曲线
        Point[] points = {new Point(0, 0), new Point(0, rand.nextInt(h)), new Point(w, rand.nextInt(h))};
        GeneralPath path = new GeneralPath();
        path.moveTo(points[0].x, points[0].y);
        for (int i2 = 0; i2 < points.length - 1; ++i2) {
            Point sp = points[i2];
            Point ep = points[i2 + 1];
            Point c1 = new Point((sp.x + ep.x) / 2, sp.y);
            Point c2 = new Point((sp.x + ep.x) / 2, ep.y);
            path.curveTo(c1.x, c1.y, c2.x, c2.y, ep.x, ep.y);
        }
        g.draw(path);

        // 画细线椭圆
        for (int n = 0; n <= 350; n += 10) {
            g.drawArc(100, 200, 600, 300, n, 5);
        }

        // 虚线正弦曲线
        int yy = 100;
        int hh = 100;
        //高度
        int aa = 200;
        for (int x = 10; x < w; x += 15) {
            int y = (int) (yy + hh * Math.sin(x * Math.PI / aa));
            //g.drawLine(x, (int) y, x, (int) y);
            g.fillOval(x, y, 6, 6);
        }


        //画虚线正圆
        g.setColor(Color.BLACK);
        int xx = 500;
        int yyy = 500;
        //坐标中心
        int angle = 0;
        // 角度
        int cw = 200, ch = 200;
        // 控制圆形的水平直径 垂直直径
        for (int n = 0; n <= 36; n++) {
            int x = xx - (int) (Math.sin(angle * Math.PI / 360) * cw);
            int y = yyy - (int) (Math.cos(angle * Math.PI / 360) * ch);
            g.fillOval(x, y, 10, 10);
            angle += 20;
        }

        // 文字水印 设置字体
        g.setColor(Color.yellow);
        try {
            Font font = Font.createFont(Font.TRUETYPE_FONT, new File("C:\\Users\\desire\\Desktop\\javaProject\\01-javaBase\\src\\main\\resources\\fonts\\f1.ttf"));
            font = font.deriveFont(Font.BOLD, 50f);
            g.setFont(font);
        } catch (FontFormatException | IOException e) {
            e.printStackTrace();
        }
        //准备文本
        String text = "这是一个文字水印";
        // 这个文字在什么位置画
        g.drawString(text, 70, 600);


        // 图片水印

        String logo = "C:\\Users\\desire\\Desktop\\javaProject\\01-javaBase\\src\\main\\resources\\水印.png";
        try {
            BufferedImage read = ImageIO.read(new File(logo));
            int lw = read.getWidth();
            int lh = read.getHeight();
            int x = 0, y = 275;
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, .5f));
            g.drawImage(read, x, y, null);
        } catch (IOException e) {
            e.printStackTrace();
        }


        //释放画笔占用内存
        g.dispose();
        // 将画好的缓冲图像,输出到硬盘上
        String p = "C:\\Users\\desire\\Desktop\\javaProject\\01-javaBase\\src\\main\\resources\\imgio.png";
        Runtime runtime = Runtime.getRuntime();
        try {
            // 将画好的缓冲图像,输出到硬盘上
            ImageIO.write(i, "png", new File(p));
            // 调用系统命令 打开图像
            runtime.exec("cmd /k start " + p);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
